package com.yungu.swift.schedule.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.base.model.admin.AdminStatusParam;
import com.yungu.swift.schedule.config.QuartzManager;
import com.yungu.swift.schedule.config.model.ScheduleJob;
import com.yungu.swift.system.marketing.service.MarketingActivityService;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.StringUtils;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ActivityQuartzActiveJob implements Job {

    @Autowired
    private QuartzManager quartzManager;
    @Reference
    private MarketingActivityService marketingActivityService;

    private static final Logger logger = LoggerFactory.getLogger(ActivityQuartzActiveJob.class);

    /**
     * 广告定时任务
     *
     * @param context JobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext context) {
        logger.info("预约广告定时任务开始执行");
        JobDetail jobDetail = context.getJobDetail();
        String jobId = (String) ParamUtil.defaultIfNull(jobDetail.getJobDataMap().get("jobId"), "");
        if (StringUtils.isNotEmpty(jobId)) {
            //设置激活
            AdminStatusParam statusParam = new AdminStatusParam();
            statusParam.setUuid(jobId);
            statusParam.setStatus(1);
            marketingActivityService.setActive(statusParam);
            //执行完之后 删除当前的任务
            ScheduleJob scheduleJob = new ScheduleJob();
            scheduleJob.setJobName(jobDetail.getKey().getName());
            scheduleJob.setJobGroup(jobDetail.getKey().getGroup());
            quartzManager.removeJob(scheduleJob);
        }
        logger.info("预约广告定时任务执行完毕");
    }
}